// mine//pages/mine_coupon/index.js
const apis = require('../../../utils/api').apis;
const formatTime = require('../../../utils/format').formatTime2;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: "",
    isInvalid: "",
    pageNo: 1,
    pageSize: 10,
    total: 0,
    dataArr: [],
    nowTime: '',
    active: 'all',
    inpVal:'',
    dayTime:86400000
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let time = formatTime(new Date())
    this.setData({
      nowTime: time,
    })
    console.log(this.data.nowTime)

    this.getCoupons()
  },
  getCoupons() {
    wx.showLoading({
      title: '加载中',
    })
    apis.couponsQueryAll({
      state: this.data.state,
      isInvalid: this.data.isInvalid,
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize
    }).then(res => {
      this.setData({
        dataArr: res.data.records,
        total: res.data.total
      })
      wx.hideLoading()
    })
  },
  switchTab(e) {
    let type = e.currentTarget.dataset.type;
    if (type == 'all') {
      this.setData({
        active: 'all',
        state: "",
        isInvalid: "",
        pageNo: 1,
        pageSize: 10,
      })
    } else if (type == 'used') {
      this.setData({
        active: 'used',
        state: "used",
        isInvalid: "",
        pageNo: 1,
        pageSize: 10,
      })
    } else if (type == 'invalid') {
      this.setData({
        active: 'invalid',
        state: "",
        isInvalid: "invalid",
        pageNo: 1,
        pageSize: 10,
      })
    }
    this.getCoupons()
  },
  valueInput(e){
    console.log(e.detail.value)
    let val=e.detail.value;
    this.setData({
      inpVal:val
    })
  },
  exchange(){
    apis.getCdkey({
      exchangeCode:this.data.inpVal
    }).then(res=>{
      if(res.data.state=='null'||res.data==null){
        wx.showToast({
          title: '没有此优惠卷',
           icon: 'none',
        })
        return
      }
      if(res.data.state=='convert'){
        wx.showToast({
          title: '已被领取或兑换',
           icon: 'none',
        })
        return
      }
      if(res.data.state=='invalid'){
        wx.showToast({
          title: '此优惠卷已失效',
           icon: 'none',
        })
        return
      }
      if(res.data.state=='success'){
        wx.showToast({
          title: '兑换成功',
        })
        this.setData({
          state: "",
          isInvalid: "",
          pageNo: 1,
          pageSize: 10,
        })
        this.getCoupons()
      }
    })
  },
  goIndex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})